import {
    fetchRoles as apiFetchRoles,
    deleteRole as apiDeleteRole,
    addRole as apiAddRole,
    updateRole as apiUpdateRole,
} from '../services/api'; // 引入 API

const role = {
    state() {
        return {
            roles: [], // 存储角色数据
        };
    },
    mutations: {
        SET_ROLES(state, roles) {
            state.roles = roles;
        },
    },
    actions: {
        fetchRoles({ commit }) {
            return apiFetchRoles().then(response => {
                commit('SET_ROLES', response.data);
                // 这里可以添加提示信息
            }).catch(() => {
                // 处理错误提示
            });
        },
        deleteRole({ dispatch }, roleId) {
            return apiDeleteRole(roleId).then(() => {
                return dispatch('fetchRoles'); // 刷新角色列表
            }).catch(() => {
                // 处理错误提示
            });
        },
        addRole({ dispatch }, roleData) {
            return apiAddRole(roleData).then(() => {
                return dispatch('fetchRoles'); // 刷新角色列表
            }).catch(() => {
                // 处理错误提示
            });
        },
        updateRole({ dispatch }, roleData) {
            return apiUpdateRole(roleData).then(() => {
                return dispatch('fetchRoles'); // 刷新角色列表
            }).catch(() => {
                // 处理错误提示
            });
        },
    },
    getters: {
        roles: (state) => state.roles,
    },
};

export default role; 